package com.village.zpark.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.village.zpark.mapper.FacilityMapper;
import com.village.zpark.pojo.Facility;
import com.village.zpark.service.FacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@Transactional
public class FacilityServiceImpl implements FacilityService {
    @Autowired
    private FacilityMapper facilityMapper;
    @Override
    public List<Facility> findAll() {
        return facilityMapper.selectAll();
    }

    @Override
    public Facility findById(Integer id) {
        return facilityMapper.selectByPrimaryKey(id);
    }

    @Override
    public int addFacility(Facility facility) {
        return facilityMapper.insert(facility);
    }

    @Override
    public int updateFacility(Facility facility) {
        return facilityMapper.updateByPrimaryKey(facility);
    }

    @Override
    public int deleteFacilityById(Integer id) {
        return facilityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo<Facility> findPage(int page, int size, String param) {
        PageHelper.startPage(page, size);
        Example example = new Example(Facility.class);
        Example.Criteria criteria = example.createCriteria();
        if (param != null && !param.isEmpty()) {
            criteria.orLike("facilityname", "%" + param + "%")
                    .orLike("location", "%" + param + "%")
                    .orLike("description", "%" + param + "%"); // 使用 orLike 来实现多字段模糊查询
        }
        List<Facility> facilities;
        if (param != null && !param.isEmpty()) {
            facilities = facilityMapper.selectByExample(example);
        } else {
            facilities = facilityMapper.selectAll(); // 当搜索栏为空时，返回全部表单信息
        }
        return new PageInfo<>(facilities);
    }
}
